package sidecar

import (
	"reflect"
	"slices"
	"strconv"
	"time"

	su "lqq/sidecar/utils"
)

type requestMsg struct {
	createTime      time.Time
	reqHeader       *RequestHeader
	reqHeaderBytes  []byte
	reqData         []byte
	semaphore       chan bool
	respHeader      *RequestHeader
	respHeaderBytes []byte
	respData        []byte
}

/*func NewRequestMsg(trackid string, serviceName string, callURL string, contentType string, c []byte) *requstMsg {
	return &requstMsg{
		reqHeader: &RequestHeader{
			TRACEID:     trackid,

			ServiceName: serviceName,
			CallURL:     callURL,
			ContentType: contentType,
		},
		reqData:   c,
		semaphore: make(chan byte),
	}
}*/

func (r *requestMsg) toRequestBytes() []byte {
	refH := reflect.TypeOf(r.reqHeader)
	refHVal := reflect.ValueOf(r.reqHeader)
	propStr := ""
	for i := 0; i < refH.NumField(); i++ {
		field := refH.Field(i)
		fn := field.Name
		ft := field.Type
		val := refHVal.FieldByName(fn)
		switch ft.Kind() {
		case reflect.String:
			if !val.IsZero() {
				v := val.Interface().(string)
				propStr = propStr + fn + "=" + v + "\n"
			}

		case reflect.Int:
			if !val.IsZero() {
				v := val.Interface().(int)
				propStr = propStr + fn + "=" + strconv.Itoa(v) + "\n"
			}
		}

	}
	logger.info("head str:" + propStr)
	headBytes := []byte(propStr)
	lenBytes := su.ComputeBytesLenToBytes(len(headBytes))
	bs := slices.Concat(lenBytes, headBytes, r.reqData)
	lenBytes = su.ComputeBytesLenToBytes(len(bs))
	return slices.Concat(lenBytes, bs)
}
